<template>
    <!-- index.wxml -->
    <view class="container">
        <view @tap="dosearch" class="search">
            <image style="width: 30px; height: 30px; vertical-align: middle; margin-right: 10rpx" src="/static/file/images/search.png"></image>
            <text>搜索商品</text>
        </view>
        <view v-for="(item, index) in Rows" :key="index">
            <blank :con="item" v-if="item.type == 'blank'"></blank>

            <tell v-else-if="item.type == 'tell'" :con="item"></tell>

            <title v-else-if="item.type == 'title'" :con="item"></title>

            <vedio v-else-if="item.type == 'vedio'" :con="item"></vedio>

            <swiper v-else-if="item.type == 'swiper'" :con="item"></swiper>

            <notice v-else-if="item.type == 'notice'" :con="item"></notice>

            <images v-else-if="item.type == 'images'" :con="item"></images>

            <coupon v-else-if="item.type == 'coupon'" :con="item"></coupon>

            <navs v-else-if="item.type == 'navs'" :con="item"></navs>

            <goods v-else-if="item.type == 'goods'" :con="item"></goods>

            <serviceItem v-else-if="item.type == 'serviceItem'" :con="item"></serviceItem>
        </view>
        <view style="height: 4rpx"></view>
    </view>
    <!-- <floatIcon></floatIcon> -->
</template>

<script>
import blank from '@/component/blank/blank';
import coupon from '@/component/coupon/coupon';
import goods from '@/component/goods/goods';
import images from '@/component/images/images';
import navs from '@/component/navs/navs';
import serviceItem from '@/component/serviceItem/serviceItem';
import swiper from '@/component/swiper/swiper';
import tell from '@/component/tell/tell';
import title from '@/component/title/title';
import vedio from '@/component/vedio/vedio';
import notice from '@/component/notice/notice';
import floatIcon from '@/component/floatIcon/floatIcon';
// pages/index/index.js
var util = require('../../utils/util.js');
var api = require('../../utils/prom.js');
var app = getApp();
var apis = require('../../appconfig.js');
export default {
    components: {
        blank,
        coupon,
        goods,
        images,
        navs,
        serviceItem,
        swiper,
        tell,
        title,
        vedio,
        notice,
        floatIcon
    },
    data() {
        return {
            timer: null,
            Rows: [],
            GtimerId0: []
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //判断是否有传入ID
        var that = this;
        uni.setStorageSync('Fpid', 0);
        if (options.Fid) {
            uni.setStorageSync('Fpid', options.Fid);
        }
        if (options.scene) {
            uni.setStorageSync('Fpid', options.scene);
        }
        app.globalData.isFrist = true;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //检查网络
        var that = this;
        console.log('isTocart========' + app.globalData.isTocart);
        console.log('isFrist========' + app.globalData.isFrist);
        if (!app.globalData.isTocart) {
            if (app.globalData.isFrist) {
                app.globalData.isFrist = false;
                app.globalData.checkNetwork(function () {
                    //开始登录
                    app.globalData.dologin(function (res) {
                        uni.setStorageSync('token', res.data.Message);
                        uni.setStorageSync('uid', res.data.User.Fid);
                        uni.setStorageSync('user', res.data.User);
                        uni.setStorageSync('store', res.data.Fstore);
                        app.globalData.qrUrl = res.data.Fstore.Fsharepic;
                        app.globalData.shareinfo.img = res.data.Fstore.Fsharepic;
                        that.getData(uni.getStorageSync('token'));
                        that.doStoreConfig(res.data.Message);
                        if (util.isEmpty(res.data.User.Fspuid)) {
                            uni.showTabBarRedDot({
                                index: 3
                            });
                        }
                    });
                });
            } else {
                if (that.Rows.length > 0) {
                    that.countDownGroups();
                }
            }
        } else {
            app.globalData.isTocart = false;
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        var that = this;
        that.GtimerId0.map(function (item, index) {
            clearTimeout(item);
        });
        that.GtimerId0= [];
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getData(uni.getStorageSync('token'));
        uni.stopPullDownRefresh();
    },
    onHide() {
        var that = this;
        clearTimeout(that.timer);
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (ops) {
        return {
            title: app.globalData.shareinfo.title,
            imageUrl: app.globalData.shareinfo.img,
            desc: app.globalData.shareinfo.desc,
            path: 'pages/index/index?Fid=' + uni.getStorageSync('uid')
            //点击分享的图片进到哪一个页面
        };
    },
    methods: {
        //获取备注
        doStoreConfig: function (token) {
            var param = {
                token: token
            };
            api.request('SpStoreConfig', 'get', param, function (res) {
                for (var i in res.data.Data) {
                    var row = res.data.Data[0];
                    if (row.Fcode == 160) {
                        uni.setStorageSync('Fwd', row.Fvalue);
                    }
                    if (row.Fcode == 170) {
                        uni.setStorageSync('Fsalemodel', row.Fvalue);
                    }
                    if (row.Fcode == 180) {
                        uni.setStorageSync('storetype', row.Fvalue);
                    }
                    if (row.Fcode == 190) {
                        uni.setStorageSync('newmember', row.Fvalue);
                    }
                    if (row.Fcode == 200) {
                        uni.setStorageSync('newuseractivity', row);
                    }
                }
            });
        },

        //获取首页列表
        getData: function (token) {
            var that = this;
            var param = {
                token: token
            };
            that.Rows=[];
            api.request('SpPage', 'get', param, function (res) {
                that.Rows  =   res.data.Rows;
                if (res.data.Rows.length > 0) {
                    that.countDownGroups();
                }
            });
        },

        // 同步更新时间
        countDownGroups: function () {
            var that = this;
            var all = that.Rows;
            all.map(function (item) {
                if (item.type == 'serviceItem') {
                    var arr = [];
                    var rowlist = [];
                    var prop = JSON.parse(item.prop);
                    if (prop.cate_id == '1') rowlist = item.AtList;
                    else if (prop.cate_id == '2') rowlist = item.AgList;
                    else if (prop.cate_id == '3') {
                        rowlist = item.AguList;
                    }
                    rowlist.map(function (row) {
                        if (prop.cate_id == '2') {
                            if (util.isEmpty(row.Fsalenum)) {
                                row.Fprogress = 0;
                            } else {
                                row.Fprogress = Number((row.Fsalenum / row.Ftotal) * 100);
                            }
                        }
                        if (row.Ftsatus == 0) {
                            var time = row.Fbtime.replace('T', ' ');
                            var index = time.indexOf('.');
                            if (index > 0) {
                                row.Fbtime = time.substr(0, index);
                            } else {
                                row.Fbtime = time;
                            }
                        } else if (row.Ftsatus == 1) {
                            var time = row.Fetime.replace('T', ' ');
                            var index = time.indexOf('.');
                            if (index > 0) {
                                row.Fetime = time.substr(0, index);
                            } else {
                                row.Fetime = time;
                            }
                            row = util.countDown(row);
                        }
                        arr.push(row);
                    });
                    if (prop.cate_id == '1') item.RowList = arr;
                    else if (prop.cate_id == '2') item.RowList = arr;
                    else if (prop.cate_id == '3') {
                        item.RowList = arr;
                    }
                }
            });
            that.Rows = all;
            var timerTem = setTimeout(function () {
                that.countDownGroups();
            }, 1000);
            that.timer= timerTem;
        },

        dosearch: function () {
            uni.navigateTo({
                url: '/pages/searchPage/searchPage'
            });
        }
    }
};
</script>
<style>
@import './index.css';
</style>
